package com.yunpuvip.sachet.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunpuvip.sachet.biz.entity.Sysparam;
import com.yunpuvip.sachet.biz.mapper.SysparamMapper;
import com.yunpuvip.sachet.biz.model.params.SysparamParam;
import com.yunpuvip.sachet.biz.model.result.SysparamResult;
import com.yunpuvip.sachet.biz.service.SysparamService;
import com.yunpuvip.sachet.modular.constant.ApplicationConstant;
import com.yunpuvip.sachet.modular.constant.CacheConstant;
import com.yunpuvip.sachet.modular.page.LayuiPageFactory;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统参数 服务实现类
 * </p>
 *
 * @author griftt
 * @since 2019-03-06
 */
@Service
public class SysparamServiceImpl extends ServiceImpl<SysparamMapper, Sysparam> implements SysparamService {

    @Override
    public void add(SysparamParam param){
        Sysparam entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SysparamParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(SysparamParam param){
        Sysparam oldEntity = getOldEntity(param);
        Sysparam newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysparamResult findBySpec(SysparamParam param){
        return null;
    }

    @Override
    public List<SysparamResult> findListBySpec(SysparamParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysparamParam param){
        Page pageContext = getPageContext();
        QueryWrapper<Sysparam> objectQueryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotNull(param.getParamKey())){
            objectQueryWrapper.like("param_key",param.getParamKey());
        }
        objectQueryWrapper.orderByDesc("create_time");
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 查询某设置参数
     *
     * @param key
     * @return Sysparam
     * @Author: Lincoln
     */
    @Override
    @Cacheable(value = CacheConstant.EcacheName.SYS_PARAM, key = "'key_'+#key")
    public Sysparam loadSetting(String key) {
        QueryWrapper<Sysparam> wrapper = new QueryWrapper<>();
        wrapper.eq("param_key", key)
                .eq("record_status", ApplicationConstant.recordStatus.normal);

        return this.getOne(wrapper);
    }

    @Override
    public List<Sysparam> getCommonDataByKey(String key) {
        if ("share_activity".equals(key)) {
            return this.getCommonData(key);
        }
        return null;
    }


    private List<Sysparam> getCommonData(String paramCode) {
        QueryWrapper<Sysparam> wrapper = new QueryWrapper<>();
        wrapper.eq("param_code", paramCode)
                .eq("record_status", ApplicationConstant.recordStatus.normal);

        List<Sysparam> sysparams = this.list(wrapper);
        for (Sysparam sysparm : sysparams) {
            sysparm.setRecordStatus(0);
            sysparm.setId(0L);
            sysparm.setParamCode("");
            sysparm.setUpdateTime(null);
        }
        return sysparams;
    }


    private Serializable getKey(SysparamParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Sysparam getOldEntity(SysparamParam param) {
        return this.getById(getKey(param));
    }

    private Sysparam getEntity(SysparamParam param) {
        Sysparam entity = new Sysparam();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
